package cn.xorange.commons.file.service;

import cn.xorange.commons.file.configuration.FileConfig;
import cn.xorange.commons.file.entity.FileBean;
import cn.xorange.commons.file.entity.FileCode;
import cn.xorange.commons.file.exception.UploadException;
import cn.xorange.commons.file.utils.FileUtil;
import cn.xorange.commons.utils.id.IdUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Objects;

/**
 * 本地文件存储
 *
 * @author ruoyi
 */
@Slf4j
@Primary
@Service
public class LocalUploadService {

    @Resource
    FileConfig fileConfig;

    public String uploadFile(String path, File file) throws UploadException {
        try {
            //String fileName = file.getOriginalFilename();
            String fileName = file.getName();//.getOriginalFilename();
            boolean check = FileUtil.checkValidFilename(fileName);
            if(Boolean.FALSE.equals(check)){
                throw UploadException.exception(FileCode.FILE_NAME_EXCEPTION);
            }
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            //生成随机文件名
            fileName = IdUtils.fastSimpleUUID() + suffix;
            //文件物理地址 d:/orange/upload
            String uploadPath = "/" + FileUtil.getUploadPath(path);
            String allUploadPath = fileConfig.localProperties().getLocalPath() + uploadPath;
            File desc = FileUtil.getUploadAbsoluteFile(allUploadPath, fileName);
            //file.transferTo(desc);
            FileUtils.copyFile(file,desc);
            String returnPath = fileConfig.localProperties().getBucketName() + uploadPath + fileName;

            log.info("文件名称={}", returnPath);
            return returnPath;
        }catch (IOException e){
            throw UploadException.ioException(e);
        }
    }

    public void delFile(String path) throws UploadException {
        if (path.startsWith(fileConfig.localProperties().getBucketName())) {
            // 前缀地址改成完整服务器真实路径
            String filePath = path.replace(fileConfig.localProperties().getBucketName(), fileConfig.localProperties().getLocalPath());
            try{
                Files.delete(Paths.get(filePath));
            }catch (IOException e){
                log.debug("本地文件上传出错"+ e.getMessage());
                throw UploadException.ioException(e);
            }
        }else {
            throw UploadException.filePathNull();
        }
    }



    public String getUrl(String path){
        return fileConfig.localProperties().getDomain() + "/" + path;

    }

}
